<template>
  <view class="userinfo-container">
    <!-- 顶部头像和昵称 -->
    <view class="avatar-name-box">

      <!-- 头像 -->
      <image class="avatar" :src="userInfo.avatar?userInfo.avatar:defaultAvatar" mode=""></image>
      <!-- 昵称 -->
      <view class="nickname">
        {{userInfo.nickName?userInfo.nickName:defaultNickName}}
      </view>
    </view>

    <!-- 面板区域 -->
    <view class="panel-box">
      <!-- 第1个面板 -->
      <view class="panel">
        <view class="panel-list">
          <view class="panel-item">
            <text class="count">8</text>
            <text>收藏的店铺</text>
          </view>
          <view class="panel-item">
            <text>14</text>
            <text>收藏的商品</text>
          </view>
          <view class="panel-item">
            <text>3</text>
            <text>关注的商品</text>
          </view>
          <view class="panel-item">
            <text>97</text>
            <text>足迹</text>
          </view>
        </view>
      </view>

      <!-- 第2个面板 -->
      <view class="panel">
        <view class="panel-title">
          我的订单
        </view>
        <view class="panel-list">
          <view class="panel-item">
            <image class="icon" src="../../static/my-icons/icon1.png" mode=""></image>
            <text>待付款</text>
          </view>
          <view class="panel-item">
            <image class="icon" src="../../static/my-icons/icon2.png" mode=""></image>
            <text>待收货</text>
          </view>
          <view class="panel-item">
            <image class="icon" src="../../static/my-icons/icon3.png" mode=""></image>
            <text>退款/退货</text>
          </view>
          <view class="panel-item">
            <image class="icon" src="../../static/my-icons/icon4.png" mode=""></image>
            <text>全部订单</text>
          </view>

        </view>
      </view>
      <!-- 第3个面板 -->
      <view class="panel">
        <view class="p-item">
          <text>收获地址</text>
          <uni-icons type="arrowright" size="16"></uni-icons>
        </view>
        <view class="p-item">
          <text>联系客服</text>
          <uni-icons type="arrowright" size="16"></uni-icons>
        </view>
        <view class="p-item" @click="logout">
          <text>退出登录</text>
          <uni-icons type="arrowright" size="16"></uni-icons>
        </view>
        <view class="p-item" @click="handlerEdit">
          <text>编辑资料</text>
          <uni-icons type="arrowright" size="16"></uni-icons>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  import {
    mapMutations,
    mapState
  } from 'vuex';
  export default {
    name: "my-userInfo",
    computed: {
      ...mapState('m_user', ['defaultAvatar', 'defaultNickName', 'userInfo', 'redirectInfo'])
    },
    data() {
      return {

      };
    },
    methods: {
      ...mapMutations('m_user', ['showAddress', 'updateToken', 'updateAvatar', 'updateNickName', 'updateRedirectInfo']),
      //修改头像和用户名
      handlerEdit() {
        uni.navigateTo({
          url: '/subpkg/edit_userInfo/edit_userInfo',
        })
      },
      //退出登录业务
      logout() {
        const that = this;
        //1. 弹出对话框,询问用户是否确认退出
        uni.showModal({
          title: '提示',
          content: '确定退出登录吗?',
          success(res) {
            //2. 对用户的点击行为进行判断,
            //如果用户点击了确认,则清除token,收货地址,用户信息
            if (res.confirm) {
              that.showAddress({});
              that.updateToken('');
              that.updateAvatar('');
              that.updateNickName('');
            }
          },
          fail(err) {
            console.error(err);
          }
        })
      },
    },
    mounted() {
      if (this.redirectInfo !== null && this.redirectInfo.openType === 'switchTab') {
        const {
          openType,
          from
        } = this.redirectInfo;

        uni.switchTab({
          url: from,
          complete: () => {
            this.updateRedirectInfo(null);
          }
        })
      }

    }
  }
</script>

<style lang="scss">
  .userinfo-container {
    height: 100%;
    background-color: #e4e4e4;

    //顶部头像和昵称
    .avatar-name-box {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      height: 450rpx;
      background-color: #c00;

      .avatar {
        width: 90px;
        height: 90px;
        border-radius: 50%;
        border: 2px solid white;
        box-shadow: 0 1px 10px black;
        margin-bottom: 10px;
      }

      .nickname {
        color: white;
        font-size: 16px;
      }
    }

    //面板区域
    .panel-box {
      padding: 0 10px;
      position: relative;
      top: -20px;

      .panel {
        background-color: white;
        border-radius: 8px;
        margin-bottom: 10px;

        .panel-list {
          font-size: 32rpx;
          display: flex;
          justify-content: space-around;
          align-items: center;

          .panel-item {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: space-between;
            padding: 10px 0;


            .icon {
              width: 30px;
              height: 30px;
            }
          }
        }

        //第2个面板标题
        .panel-title {
          font-size: 16px;
          font-weight: bold;
          line-height: 45px;
          border-bottom: 1px solid #efefef;
          padding-left: 10px;
        }

        .p-item {
          display: flex;
          line-height: 36px;
          font-size: 16px;
          justify-content: space-between;
          padding: 0 10px;
        }
      }

      &>.panel:last-child {
        margin-bottom: 0;
      }
    }
  }
</style>